package com.example.operation.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.operation.domin.Admin;
import com.example.operation.domin.Dispatch;
import com.example.operation.domin.DispatchCity;
import com.example.operation.dto.DispatchCityDto;
import com.example.operation.mapper.DispatchCityMapper;
import com.example.operation.service.DispatchCityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: DispatchCityServiceImpl
 * @Description:
 * @Author
 * @Date 2021/11/24
 * @Version 1.0
 */

@Service
public class DispatchCityServiceImpl extends ServiceImpl<DispatchCityMapper, DispatchCity> implements DispatchCityService {


    @Autowired
    private DispatchCityMapper dispatchCityMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int update(DispatchCityDto dispatchCityDto) {
        DispatchCity dispatchCity = new DispatchCity();
        LambdaUpdateWrapper<DispatchCity> lambda = new UpdateWrapper<DispatchCity>()
                .lambda().eq(DispatchCity::getDispatchId,dispatchCityDto.getDispatchId());
        Integer count = dispatchCityMapper.selectCount(lambda);
        if(count ==0) {
            throw new RuntimeException("当前配送方式有误,请修改之后再执行");
        }
        BeanUtils.copyProperties(dispatchCityDto,dispatchCity);
        dispatchCity.setAddress(dispatchCityDto.getAddress().toString());
        return dispatchCityMapper.update(dispatchCity,lambda);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int delectDyId(Integer id) {
        return dispatchCityMapper.deleteById(id);
    }
}
